const HOST = require('../../config.js').host
const app = getApp()

Page({
    data: {
        name: '',
        avatar: '',
        expressList: [],
        cartNum: 0,
        expressNum: 0,
        // isGuide: null
        isGuide: false
    },

    navigateTo (e) {
        const addr = parseInt(e.currentTarget.dataset.addr)

        switch (addr) {
            case 1:
                wx.navigateTo({
                  url: '../cart/cart',
                })
                break;
            case 2:
                wx.navigateTo({
                  url: '../user/complete/complete',
                })
                break;
            case 3:
                wx.navigateTo({
                  url: '../user/order/order',
                })
                break;
            case 4:
                wx.navigateTo({
                  url: '../user/couponList/couponList',
                })
                break;
            case 5:
              wx.navigateTo({
                url: '../user/prize/prize',
              })
              break;
            case 6:
              wx.navigateTo({
                url: '../user/question/question',
              })
              break;
            case 7:
              wx.navigateTo({
                url: '../user/ticket/auth/auth',
              })
              break;
            default:
                break;
        }
    },

    getExpressList () {
        let self = this
        wx.request({
            url: HOST + 'v1/userIndent/getListByUserId/' + app.userId + '/3',
            data: {

            },
            method: 'GET',
            success: function(res){
                if (res.data.sysCode === 1) {
                    self.setData({
                        expressList: res.data.data.slice(0, 2)
                    })
                }
            }
        })
        wx.request({
            url: HOST + 'v1/userIndent/getListByUserId/' + app.userId + '/9',
            data: {

            },
            method: 'GET',
            success: function(res){
                if (res.data.sysCode === 1) {
                    self.setData({
                        expressNum: res.data.data.length
                    })
                }
            }
        })
    },

    getCartNum () {
        let self = this
        wx.getStorage({
          key: 'cart',
          success: function(res){
            if (res.data) {
                self.setData({
                    cartNum: res.data.length
                })
            }
          }
        })
    },

    /** 生命周期 **/
    onLoad () {
        let self = this
        wx.login({
            success: function () {
                wx.getUserInfo({
                    success: function (res) {
                        app.userInfo = res.userInfo

                        self.setData({
                            avatar: res.userInfo.avatarUrl,
                            name: res.userInfo.nickName
                        })
                    
                    }
                })
            }
        })
    },

    onShow () {
        // const isGuide = app.globalData.isGuide
        // this.setData({
        //     isGuide: isGuide ? true : typeof isGuide === 'boolean' ? false : true
        // })

        this.getExpressList()
        this.getCartNum()
    }
})